package com.pet.service.impl;

import com.alibaba.excel.EasyExcel;
import com.pet.listener.PetSupListener;
import com.pet.listener.WorkerListener;
import com.pet.mapper.WorkerMapper;
import com.pet.pojo.PetSup;
import com.pet.pojo.Worker;
import com.pet.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerMapper workerMapper;



    @Override
    public List<Worker> findAll() {
        return workerMapper.findAll();
    }

    @Override
    public void add(Worker worker) {
        workerMapper.add(worker);
    }

    @Override
    public void deleteById(String id) {
        workerMapper.deleteById(id);
    }

    @Override
    public void updateWorker(Worker worker) {
        workerMapper.updateWorker(worker);
    }

    @Override
    public void batchDel(Integer[] ids) {
        workerMapper.batchDel(ids);
    }

    @Override
    public List<Worker> findByIds(Integer[] ids) {
        return workerMapper.findByIds(ids);
    }

    @Override
    public void upload(MultipartFile file) {
        // 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
        // 写法3：
        //fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";

        InputStream inputStream = null;

        //获取输入流
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, Worker.class, new WorkerListener(workerMapper)).sheet().doRead();

    }

}
